  package com.ict.dataprocess;

import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.apache.log4j.Logger;

import com.ict.config.Config;
import com.ict.utils.CacheObject;
import com.ict.utils.ConnMQUtil;
import com.ict.utils.ConnRedisUtil;
import com.ict.utils.MessageStruct;
import com.ict.utils.PublishInit;

import team.iOceanPlus.PB.CommonEnum.PB_Enum_TargetInfo_Type;
import team.iOceanPlus.PB.Target;
import team.iOceanPlus.PB.Target.PBTarget;

public class FuseRedisToMQ extends Thread {
	private static  Logger  logger=Logger.getLogger(FuseRedisToMQ.class);
	public void run() {
		//ConnMQUtil connMQUtil = new ConnMQUtil();
		try {
			Map<byte[],byte []> rt_map=ConnRedisUtil.jedis.hgetAll("mid_fuse_pos_new".getBytes());
			List<Target.PBTargetPosition> lstSendPB=new ArrayList<Target.PBTargetPosition>();
			
			for(byte [] bytPos:rt_map.values()) {
				Target.PBTargetPosition  indexPos=(Target.PBTargetPosition)byteArrayToObject(bytPos);
				lstSendPB.add(indexPos);
				
			}
			logger.info("fuse--position  filted count is"+lstSendPB.size());
			Target.PBTarget.Builder builder = Target.PBTarget.newBuilder();
			builder.setEnumSenderSoftwareValue(6);
			builder.addAllListTargetPos(lstSendPB);
			Target.PBTarget target = builder.build();
			//connMQUtil.send("topic", Config.sendExChangeName, Config.fuseTargetRoutingKey, null,target.toByteArray());
			List<MessageStruct> lstMsg = new ArrayList<MessageStruct>();
			MessageStruct msgStruct = new MessageStruct(Config.fuseTargetRoutingKey, target.toByteArray());
			lstMsg.add(msgStruct);
			//connMQUtil.sendClose();
			PublishInit.publish.Publish(lstMsg);
			logger.info("fuse-- send 2D success!");
			ConnRedisUtil.jedis.del("mid_fuse_pos_new");
			//ConnRedisUtil.jedis.del("send_fuse_pb");
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	public static Object byteArrayToObject(byte[] bytes) {
        Object obj = null;
        ByteArrayInputStream byteArrayInputStream = null;
        ObjectInputStream objectInputStream = null;
        try {
            byteArrayInputStream = new ByteArrayInputStream(bytes);
            objectInputStream = new ObjectInputStream(byteArrayInputStream);
            obj = objectInputStream.readObject();
        } catch (Exception e) {
        } finally {
            if (byteArrayInputStream != null) {
                try {
                    byteArrayInputStream.close();
                } catch (IOException e) {
                }
            }
            if (objectInputStream != null) {
                try {
                    objectInputStream.close();
                } catch (IOException e) {
                }
            }
        }
        return obj;
    }
}
